using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class Player : MonoBehaviour
{
    public const int MAX_HP = 100;
    public const int MIN_DAMAGE = 15;
    public const int MAX_DAMAGE = 25;

    public Text healthText;         // 显示血量的UI文本
    public Scrollbar healthBar;     // 血条UI (使用Scrollbar)
    public bool isLocalPlayer;      // 是否是本地玩家

    private int currentHealth;      // 当前血量
    
    public UnityEvent onDeath;      // 死亡事件

    // 添加音频相关字段
    public AudioSource audioSource;
    public AudioClip hurtSound;

    private void Awake()
    {
        // 初始化事件
        if (onDeath == null)
            onDeath = new UnityEvent();
    }

    private void Start()
    {
        currentHealth = MAX_HP;
        
        // 查找血量UI组件（如果未分配）
        FindHealthUIComponents();
        
        // 设置Scrollbar的初始值
        if (healthBar != null)
            healthBar.size = 1.0f;
            
        // 更新UI显示
        UpdateHealthUI();
    }

    // 查找血量UI组件
    private void FindHealthUIComponents()
    {
        if (healthText == null)
        {
            // 尝试在子物体中查找
            healthText = GetComponentInChildren<Text>();
            
            if (healthText == null)
            {
                // 如果是本地玩家，查找名为LocalPlayer_HealthText的对象
                // 如果是远程玩家，查找名为RemotePlayer_HealthText的对象
                string textName = isLocalPlayer ? "LocalPlayer_HealthText" : "RemotePlayer_HealthText";
                GameObject textObj = GameObject.Find(textName);
                if (textObj != null)
                    healthText = textObj.GetComponent<Text>();
            }
        }
        
        if (healthBar == null)
        {
            // 尝试在子物体中查找
            healthBar = GetComponentInChildren<Scrollbar>();
            
            if (healthBar == null)
            {
                // 如果是本地玩家，查找名为LocalPlayer_HealthBar的对象
                // 如果是远程玩家，查找名为RemotePlayer_HealthBar的对象
                string barName = isLocalPlayer ? "LocalPlayer_HealthBar" : "RemotePlayer_HealthBar";
                GameObject barObj = GameObject.Find(barName);
                if (barObj != null)
                    healthBar = barObj.GetComponent<Scrollbar>();
            }
        }
    }

    // 更新血量UI显示
    public void UpdateHealthUI()
    {
        if (healthText != null)
            healthText.text = currentHealth + "/" + MAX_HP;
            
        if (healthBar != null)
            healthBar.size = (float)currentHealth / MAX_HP;
    }

    // 受到伤害
    public void TakeDamage(int damage)
    {
        if (currentHealth <= 0) return;
        
        currentHealth -= damage;
        if (currentHealth < 0) currentHealth = 0;
        
        // 播放受伤音效
        if (audioSource != null && hurtSound != null)
        {
            audioSource.PlayOneShot(hurtSound);
        }
        
        // 更新UI
        UpdateHealthUI();
        
        // 检查是否死亡
        if (currentHealth <= 0)
        {
            onDeath.Invoke();
        }
    }

    // 获取当前血量
    public int GetHealth()
    {
        return currentHealth;
    }

    // 重置血量（用于重新开始游戏）
    public void ResetHealth()
    {
        currentHealth = MAX_HP;
        UpdateHealthUI();
    }
} 